#include "StdAfx.h"
#include ".\binbuffer.h"


CBinBuffer::CBinBuffer(void)
:m_ulPosition(0)
{
	m_vBuffer.clear();
}

CBinBuffer::~CBinBuffer(void)
{
	Reset();
}

CBinBuffer::CBinBuffer( std::vector<byte> vBuffer )
:m_vBuffer(vBuffer)
,m_ulPosition(0)
{

}

const unsigned char * CBinBuffer::GetBuffer() const
{
	return &m_vBuffer[0];
}

unsigned long CBinBuffer::GetSize() const
{
	return static_cast<unsigned long>(m_vBuffer.size());
}

unsigned long CBinBuffer::GetPosition() const
{
	return m_ulPosition;
}

void CBinBuffer::SetPosition( unsigned long ulNewPos )
{
	assert(ulNewPos < m_vBuffer.size());

	m_ulPosition = ulNewPos;
}

unsigned long CBinBuffer::ReadBytes( char *lpBuffer,unsigned long ulSize )
{
	assert(NULL != lpBuffer);
	unsigned long ulEndPos = m_ulPosition + ulSize;

	if (ulEndPos > m_vBuffer.size())
	{
		ulEndPos = static_cast<unsigned long>(m_vBuffer.size());
	}

	std::copy(&m_vBuffer[m_ulPosition],&m_vBuffer[ulEndPos],lpBuffer);

	unsigned long ulRet = ulEndPos - m_ulPosition; 

	m_ulPosition = ulEndPos; 

	return ulRet;
}

void CBinBuffer::Reset()
{
	m_ulPosition = 0;
	m_vBuffer.clear();
}

void CBinBuffer::SetBuffer( std::vector<byte> vBuffer )
{
	m_ulPosition = 0;
	m_vBuffer = vBuffer;
}

void CBinBuffer::WriteBytes( const char *lpBuffer,unsigned long ulSize )
{
	assert (NULL  != lpBuffer);

	std::copy(lpBuffer,lpBuffer + ulSize,std::back_inserter(m_vBuffer));

}
